(function($){$(window).load(function(){
//判断用户是否登录，如果登录成功，则获取cookie中的用户名并显示到登录和注册的地方；
	if($.cookie('isLogin')=='true'){
//alert($.cookie('username'));
		$('.header-user').html('<a href="javascript:;" class="login">欢迎您，15201061391</a><a href="javascript:;" class="exit">[退出]</a><span class="line">|</span><a href="javascript:;" class="register">我的订单</a><span class="line">|</span><a href="javascript:;" class="register">购物车</a><span class="line">|</span><a href="javascript:;" class="register">我的收藏</a><span class="line">|</span><span class="ico-phone"></span>400-080-1888<span class="line">|</span><a href="javascript:;" class="da">下载达令<div class="download"><span class="arrow-7-5"></span><span class="pic-code"></span></div></a>')
//点击退出按钮，清空cookie中的登录判断isLogin
		$('.exit').click(function(){
			$.cookie('isLogin',null,{path:'/'});
			window.location.href='detail.html';
		})
	}
//导航栏全部分类移入移出事件
	$('.category').hover(function(){
		$('.category .dd').css('display','block');
		$('.icon-down').css('display','inline-block');
		$('.icon-up').css('display','none');
	},function(){
		$('.category .dd').css('display','none');
		$('.icon-down').css('display','none');
		$('.icon-up').css('display','inline-block');
	})
//商品详情页物品放大效果
	$('.small-show').hover(function(){
		$('.large-show').css('display','block');
		$('.small-show .flow-layer').css('display','block');
	},function(){
		$('.large-show').css('display','none');
		$('.small-show .flow-layer').css('display','none');
	})
	var scrollTop = $(window).scrollTop();
	$(window).scroll(function(){
		scrollTop = $(window).scrollTop();
	})
	$('.small-show').mousemove(function(e){
		var evt = e || event;
		var x = evt.clientX-$(this).offset().left-$('.flow-layer').width()/2;
		var y = evt.clientY-$(this).offset().top+scrollTop-$('.flow-layer').height()/2;
		
		if(x<0){
			x=0;
		}else if(x>$(this).width()-$('.flow-layer').width()){
			x=$(this).width()-$('.flow-layer').width();
		}
		$('.flow-layer').css('left',x);
		if(y<0){
			y=0;
		}else if(y>$(this).height()-$('.flow-layer').height()){
			y=$(this).height()-$('.flow-layer').height();
		}
		$('.flow-layer').css('top',y);
		
		var percentX = x/($(this).width()-$('.flow-layer').width());
		var percentY = y/($(this).height()-$('.flow-layer').height());
		
		$('.large-show img').css({
			'left':-percentX * ($('.large-show img').width()-$('.large-show').width()),
			'top':-percentY * ($('.large-show img').height()-$('.large-show').height())}
		)
	})
	$('.x-show div').mouseenter(function(){
		var iIndex = $(this).index()+1;
		console.log(iIndex)
		$('.small-show img').attr('src','../images/LTV-Small'+iIndex+'.jpg');
		$('.large-show img').attr('src','../images/LTV-Large'+iIndex+'.jpg');
	})
	//款式选择滑动事件
	$('.good-thumbnail li').hover(function(){
		var iIndex = $(this).index();
		$(this).find('span').css('display','block');
	},function(){
		$(this).find('span').css('display','none');
	})
	//款式选择点击事件
	$('.good-thumbnail li').click(function(){
		$(this).find('b').css('display','block').end().siblings().find('b').css('display','none');
	})
	//商品购买数量加减
	var numIndex = 1;
	$('.btn-add').click(function(){
		numIndex++;
		$('.number-input').val(numIndex)
	})
	$('.btn-reduce').click(function(){
		if(numIndex<=1){
			numIndex = 1;
		}else{
			numIndex--;
			$('.number-input').val(numIndex)
		}
	})
	//立即购买点击事件
	//只有当用户登录过了，才可以正常跳转，否则自动跳转到登录页面
	if($.cookie('isLogin')=='true'){
		$('.btn-buy').click(function(){
			//同加入购物车事件基本相同;但要注意不能将标志位设为true，否则将在购物车中也有显示；
			var cartBtag = null;
			var pic = $('.small-show').find('img').attr('src');
			var title = $('.detail-data').find('h2').text();
			var price = $('.this-price').text();
			var number1 = $('.number-input').val();
			cartBtag = false;
			var inf = { 'title' : title,'src' : pic, 'price' : price, 'number' : number1, 'cartBtag' : cartBtag }
			var inf_str = JSON.stringify(inf);
			$.cookie('inf',inf_str,{ expires : 7, path : "/" });
			window.open('dingdan.html');
		})
	}else{
		//跳转到登录页面
		$('.btn-buy').click(function(){
			window.open('login.html');
		})
	}
	//评论区
	//先获取第一页的数据
	$.ajax("http://sclub.jd.com/productpage/p-1601978-s-0-t-5-p-1.html?",{
		success:function(data){
			//console.log(data.productCommentSummary.commentCount);
			//头部
			$('.comment_top a').eq(0).html('('+data.productCommentSummary.commentCount+')');
			$('.comment_top a').eq(1).html('('+data.imageListCount+')');
			$('.comment_top a').eq(2).html('('+data.productCommentSummary.goodCount+')');
			$('.comment_top a').eq(3).html('('+data.productCommentSummary.generalCount+')');
			$('.comment_top a').eq(4).html('('+data.productCommentSummary.poorCount+')');
			//插入到页面的每条数据
			for(var i = 0; i < 10 ;i++){
				$('.short_show').eq(i).find('.star').addClass('star'+data.comments[i].score);
				$('.short_show').eq(i).find('.day').append('下单'+data.comments[i].days+'天后评论');
				$('.short_show').eq(i).find('.time').append(data.comments[i].creationTime);
				$('.short_show').eq(i).find('.guige').append(data.comments[i].productColor+"&nbsp;&nbsp;"+data.comments[i].productSize);
				$('.show_content').eq(i).append(data.comments[i].content);
				$('.username img').eq(i).attr('src','http://'+data.comments[i].userImageUrl)
				$('.username-1').eq(i).append(data.comments[i].nickname);
				$('.vip_level').eq(i).append(data.comments[i].userLevelName);
				$('.from').eq(i).html('来自于'+data.comments[i].userProvince);
			}
		},
		dataType:'jsonp',
		jsonp:'callback'
	})
	//获取到当前的页码
	var pageIndex = 1;
	//滚动条滑动到指定位置
	$(".page li").click(function(){
		$('body').animate({scrollTop:780},1000)
		pageIndex = $(this).index();
		//根据页码进行数据请求
	$.ajax("http://sclub.jd.com/productpage/p-1601978-s-0-t-5-p-"+pageIndex+".html?",{
		success:function(data){
			//console.log(data.productCommentSummary.commentCount);
			//头部
			$('.comment_top a').eq(0).html('('+data.productCommentSummary.commentCount+')');
			$('.comment_top a').eq(1).html('('+data.imageListCount+')');
			$('.comment_top a').eq(2).html('('+data.productCommentSummary.goodCount+')');
			$('.comment_top a').eq(3).html('('+data.productCommentSummary.generalCount+')');
			$('.comment_top a').eq(4).html('('+data.productCommentSummary.poorCount+')');
			//插入到页面的每条数据
			for(var i = 0; i < 10 ;i++){
				$('.short_show').eq(i).find('.star').addClass('star'+data.comments[i].score);
				$('.short_show').eq(i).find('.day').html('下单'+data.comments[i].days+'天后评论');
				$('.short_show').eq(i).find('.time').html(data.comments[i].creationTime);
				$('.short_show').eq(i).find('.guige').html(data.comments[i].productColor+"&nbsp;&nbsp;"+data.comments[i].productSize);
				$('.show_content').eq(i).html(data.comments[i].content);
				$('.username img').eq(i).attr('src','http://'+data.comments[i].userImageUrl)
				$('.username-1').eq(i).html(data.comments[i].nickname);
				$('.vip_level').eq(i).html(data.comments[i].userLevelName);
				$('.from').eq(i).html('来自于'+data.comments[i].userProvince);
			}
		},
		dataType:'jsonp',
		jsonp:'callback'
		})
	})
//切换商品详情页中的详情、评论、售后三个模块
	$('.detail-all li').click(function(){
		$('body').animate({scrollTop:780},1000)
		var iIndex = $(this).index();
		$('.show-hide').css('display','none').eq(iIndex).css('display','block');
	})
//设置一个吸顶
$(window).scroll(function(){
	if($(window).scrollTop() >= 792){
		$('.detail-all').css({
			'position':'fixed',
			'top':0,
			'left':32
		})
	}
	else{
		$('.detail-all').css({
			'position':'relative',
			'left':0
		});
	}
})
//右侧-大家都在看  轮播图
//大家都在买侧边点击更换图片
	var arrowIndex = 0;
	$('.arrow-left').click(function(){
		arrowIndex--;
		if(arrowIndex<0){
			arrowIndex = 2;
		}
		$('.all-watch ul').css('display','none').eq(arrowIndex).css('display','block');
	})
	$('.arrow-right').click(function(){
		arrowIndex++;
		if(arrowIndex>2){
			arrowIndex = 0;
		}
		$('.all-watch ul').css('display','none').eq(arrowIndex).css('display','block');
	})
//判断cookie中购物车的数量，并同步到页面
	$('.banner-cart-count').text($.cookie('count'));
//将要加入购物车的数据先放入cookie中

	//购物车中数量的计数器
	var count = 0;
	$('.cart-add').click(function(){
		count++;
		var cartBtag = null;
		var pic = $('.small-show').find('img').attr('src');
		var title = $('.detail-data').find('h2').text();
		var price = $('.this-price').text();
		var number1 = $('.number-input').val();
		cartBtag = true;
		var inf = { 'title' : title,'src' : pic, 'price' : price, 'number' : number1, 'cartBtag' : cartBtag }
		var inf_str = JSON.stringify(inf);
		$.cookie('inf',inf_str,{ expires : 7, path : "/" });
		//将购物车中的商品数量加入cookie中，这样可以使其同步显示到其他页面中
		$.cookie('count',count,{expires:7,path:'/'});
		$('.banner-cart-count').text(count);
	})
	
//后添加上的小米搜索框;
	var	oSeachContent = $('.s-keyword');
	var	oSearchList   = $('#search-list');
	var	iSearchIndex  = -1;
	var	sOriHtml 	  = oSearchList.html();
	oSeachContent.focus(function () {
		oSearchList.css('display', 'block');
	}).blur(function () {
		oSearchList.css('display', 'none');
	}).keyup(function (ev) {
		var
			ev = ev || window.event,
			aLi = $('#search-list li');
		if(ev.keyCode === 38 || ev.keyCode === 40) {
			if(ev.keyCode === 38 && iSearchIndex > 0) {
				iSearchIndex--;
			} else if(ev.keyCode === 40 && iSearchIndex < aLi.length - 1) {
				iSearchIndex++;
			}
			oSeachContent.val(aLi.removeClass('active').eq(iSearchIndex).addClass('active').data('key'));
		}
	}).on('input propertychange', function () {
		var sSearchCon = $(this).val();
		if(sSearchCon) {
			$.ajax({
				url: 'http://search.mi.com/search/expand',
				data: {keyword: sSearchCon},
				jsonp: 'jsonpcallback',
				dataType: 'jsonp',
				success: function (data) {
					var sHtml = '';
					data.forEach(function (v) {
						sHtml += '<li data-key="' + v.Key + '"><a href="javascript:;"><span>约有' + v.Rst + '件</span>' + v.Key + '</a></li>';
					});
					if(sHtml) {
						oSearchList.css('display', 'block').html(sHtml);
					} else {
						oSearchList.css('display', 'none');
					}
					iSearchIndex = -1;
				}
			});
		} else {
			oSearchList.html(sOriHtml);
			iSearchIndex = -1;
		}
	});
})
})(jQuery)
